// +----------------------------------------------------------------------
// | CodeEngine
// +----------------------------------------------------------------------
// | Copyright 艾邦
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: TaoGe <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
// | Version: 2.0 2019/7/11 15:22
// +----------------------------------------------------------------------
'use strict';

import * as C from './config';

// 本地配置
const storageConfig = {
  authorize: 'jt_user_token',
  user_info: 'jt_user_info',
};

// 服务器配置
const serverConfig = {

  // 终端名称
  terminal: C.terminal,

  // 接口版本
  version: C.version,

  // appId
  app_id: C.appId,

  // APP秘钥
  app_secret: C.appSecret,

  // 传输协议,可选http https
  transfer_protocol: C.transferProtocol,

  // 域名
  domain: C.domain,

  // 应用程序接口地址
  host: C.host,

  // 接口是否签名加密
  is_sign: C.isSign,

  // Referer校验
  referer: C.referer,

  // 是否超时验证
  times_out: C.timesOut,

  // state防止CSRF
  state: C.state,

  // 通信秘钥
  key: C.key,

  // 加密方式
  sign_type: C.signType,

  // RSA公钥
  public_key: C.rsaCert.public_key,

  // RSA私钥
  private_key: C.rsaCert.private_key
};

// 默认配置
const defaultConfig = Object.assign(serverConfig, storageConfig);

/**
 * 配置文件实现单例模式
 */
export default class Config {

  constructor(config = {}) {
    this.config = Object.assign(defaultConfig, config);
  }

  static setConfig(config = {}) {
    const oldConf = this.getInstance().getConfig();
    const newConf = Object.assign(oldConf, config);
    this.instance = new Config(newConf);
    return this.getInstance().getConfig()
  }

  getConfig() {
    return this.config;
  }

  static getInstance(config = {}) {
    if (!this.instance) {
      this.instance = new Config(config);
    }
    return this.instance;
  }

}
